local player_class = ac.player.__index

--玩家刷怪点
function player_class:getAtkPoint()
    local hero = self.hero
    local point = self.enemy_point - {math.random(360),math.random(150,450)}
    return point
end

--[玩家添加怪物]
function player_class:create_enemy(name,point,level)
    local hero = self.hero
    local owner = self.enemy_player
    local point = point or self:getAtkPoint()
    local unit = owner:create_unit(name,point)
    unit:set_enemy_level(level or unit['默认等级'] or self.enemy_level)
    unit:issue_order('attack',hero or self.point1)
    unit.owner_player = self
    return unit
end

--[清空刷怪]
function ac.player.__index:clear_enemy()
    for u in ac.selector('unit')
        : range(ac.point(0,0),20000)
        : can_god()
        : loop()
    do
        if u.owner_player==self then
            u:remove()
        end
    end
    self.enemy_count = 0
    if self.sb_timer then
        self.sb_timer:remove()
        self.sb_timer = nil
    end
end

--[持续刷怪]
player_class.enemy_speed = 5
player_class.enemy_count = 0
player_class.enemy_max = 5
player_class.enemy_level = 0
player_class.enemy_unit = '小怪模板'
--[开启刷怪]
function ac.player.__index:open_enemy()
    local speed = self.enemy_speed
    local time = 1/speed
    if self.sb_timer then
        self.sb_timer:remove()
    end
    self.sb_timer = ac.loop(time * 1000,function()
        if self.enemy_count>=self.enemy_max then
            return 
        end
        
        self.enemy_count = self.enemy_count + 1

        local name = self.enemy_unit
        local unit = self:create_enemy(name)
        unit:add_effect([[Abilities\Spells\Undead\RaiseSkeletonWarrior\RaiseSkeleton.mdl]]):remove()
        unit:set('移动速度',math.floor(self.enemy_level/6)+100)
        unit:event '单位-死亡'(function()
            self.enemy_count = self.enemy_count - 1
        end)

        --速度变动立刻重置
        if speed~=self.enemy_speed then
            self:open_enemy()
        end
    end)
end


local mt = {}
--难度
mt.hard = 0

--怪物强度
mt.level = 0
mt.max_level = 1800

--变动间隔
mt.time1 = 5
--boss刷新
mt.time2 = 330
--失败计时
mt.time3 = 150

mt.boss_index = 0
mt.boss_max = 5


--boss
function mt:wait_boss(time)
    self.boss_index = self.boss_index + 1
    if self.exit_timer2 then self.exit_timer2:remove() end

    self.exit_timer2 = ac.wait(self.time2*1000,function(t)
        local boss = ('首领%s'):format(self.boss_index)
        if self.boss_index<self.boss_max then
            self:wait_boss()
            ac.each_player(function(player)
                local unit = player:create_enemy(boss)
                player:notify('玩家-添加Boss',player,self.boss_index)
            end)
        else
            self:clear()
            ac.game:notify('游戏-关闭挑战')
            ac.game:notify('游戏-停止刷怪')
            ac.each_player(function(player)
                player:notify('玩家-传送',player,player.point1)
                player:set_camera(unit:get_point(),0.1)
            end)

            --最终boss
            local unit = ac.player[12]:create_unit(boss,ac.base_point)
            unit:set_enemy_level(nil,boss)
            unit:add_buff 'Boss出场'{}
            unit:event '单位-死亡'(function()
                ac.game:notify('游戏-游戏胜利',self)
            end)

            --失败计时
            self.exit_timer2 = ac.wait(self.time3*1000,function(t)
                ac.game:notify('游戏-游戏失败',self)
            end)
            ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'游戏失败')
        end
    end)
    self.title = ('正常模式 | 难%s'):format(self.hard)
    if self.boss_index==self.boss_max then
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'决战：')
    else
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,('首领%s：'):format(self.boss_index))
    end
end

--出怪
function mt:loop_unit()
    if self.exit_timer0 then self.exit_timer0:remove() end
    if self.exit_timer1 then self.exit_timer1:remove() end

    self.exit_timer0 = ac.loop(self.time1*1000,function(t)
        self.level = self.level + self.time1
        ac.each_player(function(player)
            player.enemy_level = self.level
        end)
        if self.level>=self.max_level then
            t:remove()
            ac.game:notify('游戏-停止出怪',self)
        else
            ac.game:notify('游戏-新的一波',self,self.level)
        end
    end)
    self.exit_timer0:on_timer()

    --精英怪数量
    local time = 30
    self.exit_timer1 = ac.loop(time*1000,function()
        ac.each_player(function(player)
            local unit = player:create_enemy('巨型骷髅')
            unit['木材'] = math.random(10,20)
        end)
    end)
    
end


function mt:clear()
    if self.exit_timer0 then self.exit_timer0:remove() end
    if self.exit_timer1 then self.exit_timer1:remove() end
    if self.exit_timer2 then self.exit_timer2:remove() end
    --for u in pairs(self.units) do
    --    u:remove()
    --end
    ac.each_player(function(player)
        player.enemy_unit = '骷髅战士'
        player:clear_enemy()
    end)
end

--[游戏时间]
function mt:get_time()
    return ac.clock() - self.start_time
end

function mt:init(time)
    local time = time or 10
    self.units = {}
    self.start_time = ac.clock()
    self.exit_timer1 = ac.wait(time*1000,function()
        self:loop_unit()
        self:wait_boss()
        ac.each_player(function(player)
            player.enemy_point:add_effect([[QY_csm.mdl]])
            player:open_enemy()
        end)
        ac.game:notify('游戏-开始刷怪',self)
    end)

    --每分钟增加1怪物上限，最高+20
    ac.timer(60*1000,20,function()
        ac.each_player(function(player)
            player.enemy_max = player.enemy_max + 1
        end)
    end)
    
end

ac.game:event '游戏-停止刷怪'(function()
    ac.each_player(function(player)
        player:clear_enemy()
    end)
end)


ac.game:event '玩家-离开游戏'(function(_,player)
    player:clear_enemy()
end)


ac.game:event '游戏-设置难度'(function(_,hard)
    local data = setmetatable({}, {__index = mt})
    ac.enemy = data
    data.hard = hard
    data:init(0)
end)


local test_data = {
    {name = '关闭出怪',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
    {name = '强化怪物',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
    {name = '刷Boss',art = [[ReplaceableTextures\CommandButtons\BTNCancel.blp]]},
}

ac.wait(10,function()
    ac.game:notify('测试-注册数据','测试流程',test_data,function(_,player,name)
        if name=='关闭出怪' then
            ac.enemy:clear()
            if player.sb_timer then
                player.sb_timer:remove()
            end
        elseif name=='强化怪物' then
            if ac.enemy.exit_timer0 then
                ac.enemy.level = ac.enemy.level + 9
                ac.enemy.exit_timer0:on_timer()
            end
        elseif name=='刷Boss' then
            ac.enemy.exit_timer2:on_timer()
        end
    end)


end)


return mt